from pychecktype.checked import checked

from db.dao import OrderDao
from db.models import StockOrder
from quantification.data_handler.DataHandler import DataHandler
from utils.util import CommonUtil, ObjDictTool


class Order(DataHandler):
    __slots__ = ('stock_code', 'deal_direction', 'plans', 'price', 'order_state', 'order_id')

    def __init__(self, stock_code, stock_name, deal_direction, plan_nums, price):
        self.order_id = CommonUtil.get_uuid()
        self.stock_code = stock_code
        self.stock_name = stock_name
        # 交易方向1：买入，-1：卖出  0:不操作
        self.deal_direction = deal_direction
        # 计划购买/卖出股票数
        self.plans = plan_nums
        # 股价
        self.price = price
        # 订单状态
        self.order_state = 0

    def load_data(self):
        pass

    def store_data(self):
        stock_order = StockOrder()
        ObjDictTool.copyA2B(self, stock_order)
        # setattr(stock_order,'plans',self.plans)
        OrderDao().save(stock_order)

    @checked
    def change_order_state(self, state: int):
        self.order_state = state
        self.store_data()
